package com.xoa.service.sys.impl;

import com.ibatis.common.resources.Resources;
import com.xoa.dao.common.SysParaMapper;
import com.xoa.dao.unitmanagement.UnitManageMapper;
import com.xoa.dao.version.VersionMapper;
import com.xoa.model.common.SysPara;
import com.xoa.model.users.OrgManage;
import com.xoa.service.sys.SystemInfoService;
import com.xoa.service.users.OrgManageService;
import com.xoa.service.users.UsersService;
import com.xoa.util.Register;
import com.xoa.util.common.StringUtils;
import com.xoa.util.ipUtil.MachineCode;
import com.xoa.util.systeminfo.DESUtils;
import com.xoa.util.systeminfo.MachineUtil;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @创建作者: 韩成冰
 * @创建日期: 2017/5/31 19:26
 * @类介绍: 系统信息
 * @构造参数: 无
 **/
@Service
public class SystemInfoServiceImpl implements SystemInfoService {
    @Resource
    private UnitManageMapper unitManageMapper;
    @Resource
    private UsersService usersService;
    @Resource
    private OrgManageService orgManageService;

    @Resource
    private SysParaMapper sysParaMapper;
    @Autowired
    private VersionMapper versionMapper;

    @Value("${xoa_softName}")
    String softName;
    @Value("${xoa_cpright}")
    String cpright;
    @Value("${xoa_weblocation}")
    String weblocation;
    //加密密钥，后期可以根据不同的公司，设置不同的秘钥，注意生成授权文件中的是这个，要是改变，都要变
    public static final String MY_KEY = "gaosuboisnumber1";//"gaosuboisnumber1";//"gaosuboxoa2019";
    public static final String NEW_KEY2020 = "8oa.cngaosubo123";//"gaosuboisnumber1";//"gaosuboxoa2019";
    //系统版本
    public static final String softVersion = "2020.11.11.1";
    //版本号
    public static final String softVersionNO = "1.7";
    public static String webPort = "8080";
    public static final String webServer = "apache-tomcat-7.0.53";
    public static String dbName = "Mysql";
    public static final String dbVersion = "5.6";
    public static final String timezone = "中国标准时间 (8 GMT)";
    public static String isSoftAuth = "未授权";
    public static String useCom = "5";
    public static String dbMysql = "com.mysql.jdbc.Driver";
    public static String dbOracle = "oracle.jdbc.driver.OracleDriver";
    public static String dbDM = "dm.jdbc.driver.DmDriver";

    //服务器路径
    public static String serverPath = "";
    //正式授权信息说明文字
    public static final String reloadAuthorizationstr = "正式授权信息" +
            "（正式授权信息，需校验组织机构名称、机器码和软件序列号，务必与授权文件中的信息一致）";


    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/5/31 19:27
     * @函数介绍: 获取系统信息
     * @参数说明: @param 无
     * @return: Map<String, String>
     **/
    public Map<String, String> getSystemInfo(String path, Object locale, HttpServletRequest request) throws ParseException {

        ResourceBundle rb = ResourceBundle.getBundle("jdbc-sql");

        if (dbOracle.equals(rb.getString("driverClassName"))) {
            dbName = "Oracle";
        } else if (dbMysql.equals(rb.getString("driverClassName"))) {
            dbName = "Mysql";
        } else if (dbDM.equals(rb.getString("driverClassName"))) {
            dbName = "DM";
        }
        webPort = String.valueOf(request.getLocalPort());
        Map<String, String> map = new HashMap<String, String>();
        //版权所有
        map.put("cpright", cpright);
        //官方网站
        map.put("weblocation", weblocation);
        //WEB端口
        map.put("webPort", webPort);
        ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
        String serverInfo = servletContext.getServerInfo();
        //WEB服务器
        map.put("webServer", serverInfo);
        //数据库名
        map.put("dbName", dbName);
        //数据库版本
        map.put("dbVersion", dbVersion);
        //语言
        if (locale == null) {
            map.put("language", "zh_CN");
        } else {
            map.put("language", locale.toString());
        }

        //时区
        map.put("timezone", timezone);

        //已用单位数
        List<OrgManage> orgManageList = orgManageService.getOrgManage();

        if (orgManageList != null) {
            useCom = orgManageList.size() + "";
        }
        //使用单位数
        map.put("useCom", useCom);


        //软件版本
        map.put("softVersion", softVersion);

        //数据库中的版本
        String dataversion = versionMapper.selectVer();

        map.put("dataversion", dataversion);

        // map.put("softVersionNO", softVersionNO);
        //操作系统
        map.put("operationSystem", getOperationSystem());

        //map.put("serverPath", serverPath);

        //jdk版本


        map.put("jdkVersion", getJdkVersion());
        //  map.put("machineCode", MachineUtil.get16Machine());
        //查询用户数
        int usercount = usersService.getUserCount();
        //允许登录用户数
        int canLoginUser = usersService.getCanLoginUser();
        int notLoginUser = usercount - canLoginUser;
        //用户情况
        map.put("userInfo", "总用户" + usercount + "  (允许登录用户数" + canLoginUser + "人，  禁止登录用户数" + notLoginUser + ")");


        map.put("reloadAuthorizationstr", reloadAuthorizationstr);
        //授权信息
        Map<String, String> infoMap = getAuthInfo(path);
        if (infoMap==null||infoMap.size()==0){
            return null;
        }

        //注册日期
        String lecDateStr = infoMap.get("lecDateStr");
        lecDateStr = lecDateStr + " 00:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = sdf.parse(lecDateStr);
        Long addTime = 365 * 24 * 60 * 60 * 1000L;
        Long time = startDate.getTime() + addTime;
        Date endDate = new Date(time);
        String endDateStr = sdf.format(endDate);
        endDateStr = endDateStr.substring(0, 10);
        //服务截止日期
        map.put("endLecDateStr", infoMap.get("endDay"));

        if ("true".equals(infoMap.get("isAuthStr"))) {
            map.put("softName", getSoftName(true));
            //软件序列号
            map.put("softSerialNo", infoMap.get("sn_no"));
            String sn_no = infoMap.get("sn_no");
            String newStr = new String();
            if (!StringUtils.checkNull(sn_no)) {
                Calendar date = Calendar.getInstance();
                String year = String.valueOf(date.get(Calendar.YEAR));
                int len = sn_no.indexOf(year);
                //截取
                //判断如果包含并是当前年份
                //这个判断看起来没啥意义，暂添加2019年判断
                if (!sn_no.contains(year) || !sn_no.contains("2018")) {
                    String[] split = sn_no.split(year);
                    if (split.length <= 1) {
                        split = sn_no.split("2019");
                        if (split.length <= 1) {
                            split = sn_no.split("2018");
                        }
                    }
                    //这个是A2233443后面的数据
                    if (split.length > 1) {
                        String str = split[1].substring(0, 1);
                        switch (str) {
                            case "A":
                                if (infoMap.get("users").equals("不限制")) {
                                    newStr = "大企业版";
                                    break;
                                } else if (Integer.valueOf(infoMap.get("users")) > 0 && Integer.valueOf(infoMap.get("users")) < 50) {
                                    newStr = "小微企业版";
                                    break;
                                }
                                if (Integer.valueOf(infoMap.get("users")) >= 50 && Integer.valueOf(infoMap.get("users")) < 100) {
                                    newStr = "小企业版";
                                    break;
                                } else if (Integer.valueOf(infoMap.get("users")) >= 100 && Integer.valueOf(infoMap.get("users")) < 200) {
                                    newStr = "企业标准版";
                                    break;
                                } else if (Integer.valueOf(infoMap.get("users")) >= 200) {
                                    newStr = "大企业版";
                                    break;
                                }
                                break;
                            case "X":
                                if (Integer.valueOf(infoMap.get("users")) > 0 && Integer.valueOf(infoMap.get("users")) <= 200) {
                                    newStr = "中小企业版 200用户";
                                    map.put("freeCompany", "0");
                                    String lecDateStr1 = infoMap.get("endDay");
                                    lecDateStr1 = lecDateStr1 + " 00:00:00";
                                    Date startDate1 = sdf.parse(lecDateStr1);
                                    if (startDate1 != null) {
                                        int day = getDay(startDate1);
                                        if (day > 864000) {
                                            map.put("show", "显示不续约");
                                        } else {
                                            map.put("show", "显示续约");
                                        }
                                    }
                                    break;
                                }
                                break;
                            case "D":
                                newStr = "标准版";
                                break;
                            case "E":
                                newStr = "旗舰版 不限用户版";
                                break;
                            case "F":
                                newStr = "平台版 不限用户版";
                                break;
                            case "G":
                                newStr = "政务版 不限用户版";
                                break;
                            case "H":
                                newStr = "集团版 不限用户版";
                                break;
                            case "J":
                                newStr = "教育版 不限用户版";
                                break;
                            case "S":
                                newStr = "智慧城镇版 不限用户版";
                                break;
                            case "T":
                                if (Integer.valueOf(infoMap.get("users")) > 0 && Integer.valueOf(infoMap.get("users")) <= 20) {
                                    newStr = "天翼云OA 20用户免费版";
                                }
                                break;
                        }
                    }
                } else {
                    //这个是A2233443后面的数据
                    //不包含当前年的截取前5个  比如是之前年的授权文件
                    String substring = sn_no.substring(5, sn_no.length());
                    //这个是字母ABCD
                    String str = substring.substring(0, 1);
                    switch (str) {
                        case "A":
                            if (infoMap.get("users").equals("不限制")) {
                                newStr = "大企业版";
                                break;
                            } else if (Integer.valueOf(infoMap.get("users")) > 0 && Integer.valueOf(infoMap.get("users")) < 50) {
                                newStr = "小微企业版";
                                break;
                            }
                            if (Integer.valueOf(infoMap.get("users")) >= 50 && Integer.valueOf(infoMap.get("users")) < 100) {
                                newStr = "小企业版";
                                break;
                            } else if (Integer.valueOf(infoMap.get("users")) >= 100 && Integer.valueOf(infoMap.get("users")) < 200) {
                                newStr = "企业标准版";
                                break;
                            } else if (Integer.valueOf(infoMap.get("users")) >= 200) {
                                newStr = "大企业版";
                                break;
                            }
                            break;
                        case "X":
                            if (Integer.valueOf(infoMap.get("users")) > 0 && Integer.valueOf(infoMap.get("users")) <= 200) {
                                newStr = "中小企业版 200用户";
                                map.put("freeCompany", "0");
                                String lecDateStr1 = infoMap.get("endDay");
                                lecDateStr1 = lecDateStr1 + " 00:00:00";
                                Date startDate1 = sdf.parse(lecDateStr1);
                                if (startDate1 != null) {
                                    int day = getDay(startDate1);

                                    if (day > 864000) {
                                        map.put("show", "显示不续约");
                                    } else {
                                        map.put("show", "显示续约");
                                    }
                                }
                                break;
                            }
                            break;
                        case "D":
                            newStr = "标准版";
                            break;
                        case "E":
                            newStr = "旗舰版 不限用户版";
                            break;
                        case "F":
                            newStr = "平台版 不限用户版";
                            break;
                        case "G":
                            newStr = "政务版 不限用户版";
                            break;
                        case "H":
                            newStr = "集团版 不限用户版";
                            break;
                        case "J":
                            newStr = "教育版 不限用户版";
                            break;
                        case "S":
                            newStr = "智慧城镇版 不限用户版";
                            break;
                        case "T":
                            if (Integer.valueOf(infoMap.get("users")) > 0 && Integer.valueOf(infoMap.get("users")) <= 20) {
                                newStr = "天翼云OA 20用户免费版";
                            }
                            break;

                    }
                }
            }
            //根据序列号增加软件名称
            if (StringUtils.checkNull(newStr)) {
                map.put("softNoName", "小微企业版");
            } else {
                map.put("softNoName", newStr);
            }
        } else {
            map.put("softName", getSoftName(false));
            map.put("softNoName", "小微企业版");
            map.put("freeCompany", "1");
            //软件序列号
            map.put("softSerialNo", "无");

        }

        //授权单位数
        map.put("company_num", infoMap.get("company_num"));
        //授权单位
        map.put("authorizationUnit", infoMap.get("company"));
        //  map.put("orgName", infoMap.get("company"));
        //先去判断软件是否注册
        if ("true".equals(infoMap.get("isAuthStr"))) {
            //授权用户数
            map.put("oaUserLimit", infoMap.get("users"));
        } else {
            map.put("oaUserLimit", "30");
        }

        // map.put("authorizationBeginDate", infoMap.get("startTime"));
        // map.put("authorizationEndDate", infoMap.get("endDay"));
        //移动终端限制
        //map.put("mobileTeriminalLimit", infoMap.get("users"));
        //移动终端限制
        //map.put("pcTerminalLimit", infoMap.get("users"));
        //  map.put("sn_no", infoMap.get("sn_no"));

        //机器码16位
        String machineCode = this.getMachineCode16();;
        map.put("authorizationCode", machineCode);

       /* if (MachineCode.get16CharMacsType() != null && MachineCode.get16CharMacsType().size() > 0) {
            String str = MachineCode.get16CharMacsType().get(0);
            map.put("authorizationCode", str);
        } else {
            map.put("authorizationCode", "");
        }*/


        //map.put("endTime", infoMap.get("remainderDay"));

        //安装日期
        map.put("installDate", infoMap.get("installDate"));


        //是否授权（注册）
        String isAuthStr = infoMap.get("isAuthStr");
        if ("true".equals(isAuthStr)) {
            map.put("isSoftAuth", "已注册");

        } else {
            map.put("isSoftAuth", "未注册(小微企业版无需注册)");
            map.put("endLecDateStr", null);
        }

        //试用截止日期
        if ("true".equals(isAuthStr)) {
            //永久使用为null
            map.put("useEndDate", null);
        } else {
            String installDateStr = infoMap.get("installDate");
            installDateStr = installDateStr + " 00:00:00";
            Date startDate0 = sdf.parse(installDateStr);
            String tryDay = infoMap.get("tryDay");
            int trydayTime = 30;
            try {
                trydayTime = Integer.parseInt(tryDay);
            } catch (Exception e) {
            }

            Long addTime1 = trydayTime * 24 * 60 * 60 * 1000L;
            Long time1 = startDate0.getTime() + addTime1;
            Date endDate1 = new Date(time1);
            String endDateStr3 = sdf.format(endDate1);
            endDateStr3 = endDateStr3.substring(0, 10);
            map.put("useEndDate", endDateStr3);
        }


        if (map.get("isSoftAuth").contains("小微企业版")) {
            //注册日期
            map.put("lecDateStr", "无");
        } else {
            //注册日期
            map.put("lecDateStr", infoMap.get("lecDateStr"));
        }
        //版本
        map.put("edition", infoMap.get("edition"));
        //模块权限
        map.put("module", infoMap.get("module"));
        return map;
    }

    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/6/26 16:37
     * @函数介绍: 获取操作系统信息
     * @参数说明: 无
     * @return: String
     **/
    public String getOperationSystem() {

        Properties prop = System.getProperties();
        String osName = (String) prop.get("os.name");
        if (osName.startsWith("Windows") || osName.startsWith("windows")) {
            osName = "Windows";
        }
        return osName; //operationSystem;
    }


    public String getJdkVersion() {
        return System.getProperty("java.version");
    }

    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/6/26 16:36
     * @函数介绍: 从授权文件中获取授权信息, 根据授权与否设置不同。
     * 授权文件里的信息解密后用逗号隔开。依次是
     * 公司名，企业数，授权人数，授权开始时间，授权结束时间，软件序列号，机器码，授权使用时间。
     * xxx公司,5,30,2017-06-28,2017-08-31,xoa17-12345678-1234,L1HF52F003PAAAAA,30
     * @参数说明: @param 无
     * @return: Map<String, String></String,>
     **/
    public Map<String, String> getAuthInfo(String path) {

        //注册文件内容
        String registerContent = null;
        //获取文件名path
        String filePath = Register.getDefaultFileName();
        StringBuffer sb = new StringBuffer();
        File file = new File(path.concat(filePath));
        //判断是否有这个文件
        if (!file.exists()) {
            //如果没有这个文件，直接取静态默认注册文件内容
            registerContent = Register.getDefaultContent();
        } else {
            //如果有的话。读取这个dat文件。
            try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));) {

                registerContent = br.readLine();
                if (registerContent == null || registerContent.trim().length() == 0) {
                    registerContent = Register.getDefaultContent();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Map<String, String> hashMap = null;
        try {
            //添加2019版授权信息不能在2020版验证通过
            if(!registerContent.equals(Register.getDefaultContent())){
                String check2020 = newDec(registerContent);
                String realMachineCode = "4CCC6A6247B84C66";
                if (!check2020.contains(realMachineCode.substring(0,12))){
                    registerContent = Register.getDefaultContent();
                }
            }

            String decryptData = DESUtils.finalDec(registerContent);
            String[] infoArr = decryptData.split(",");

            String company = infoArr[0];
            String company_num = infoArr[1];
            String users = infoArr[2];
            String startTimeInfo = infoArr[3];
            String endDay = infoArr[4];
            String snno = infoArr[5];
            String machineCode = infoArr[6];

            String tryTime = infoArr[7];
            String edition = "";
            if (infoArr.length > 9 && !StringUtils.checkNull(infoArr[9])) {
                edition = infoArr[9];
            }
            String module = "";
            if (infoArr.length > 8 && !StringUtils.checkNull(infoArr[8])) {
                if (infoArr[8].indexOf("~") > -1) {

                    module = infoArr[8].replace("~", ",");
                } else {
                    module = infoArr[8];
                }

            }
            hashMap = new HashMap<String, String>();

            //授权模块
            hashMap.put("module", module);
            //授权模块
            hashMap.put("edition", edition);
            //试用时间,
            hashMap.put("tryDay", tryTime);
            //安装时间
            File file1 = new File(path.concat("/authfiles/temp"));
            long lastModified1 = file1.lastModified();
            Date installDate1 = new Date(lastModified1);
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            String installDateStr = sdf1.format(installDate1);
            hashMap.put("installDate", installDateStr);


            long lastModified = file.lastModified();
            Date installDate = new Date(lastModified);
            String lecDateStr = sdf1.format(installDate);
            hashMap.put("lecDateStr", lecDateStr);

            hashMap.put("company", company);
            hashMap.put("company_num", company_num);

            //0表示不限制
            if (Integer.parseInt(users) == 0) {
                users = "不限制";
            } else if (Integer.parseInt(users) == 200) {
                hashMap.put("freeCompany", "0");
            }
            hashMap.put("users", users);

            hashMap.put("startTime", startTimeInfo);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String startDay = null;

            //是否是授权版，开始时间不一样。
            boolean isAuth = checkMachineCode(machineCode);

            if (isAuth) {
                hashMap.put("isAuthStr", "true");

            } else {
                hashMap.put("isAuthStr", "false");
                hashMap.put("users", "30");
            }

            //永久使用
            if (endDay.equals("0000-00-00") || endDay.equals("2020-01-01")) {
                endDay = "2040-01-01";
                hashMap.put("endDay", endDay);
            } else {
                hashMap.put("endDay", endDay);
            }

            hashMap.put("sn_no", snno);

            String encryptMaCode = DESUtils.encrypt(machineCode, MY_KEY);

            while (encryptMaCode.length() < 16) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(encryptMaCode);
                stringBuffer.append(encryptMaCode.substring(0, 1));
                stringBuffer.append(encryptMaCode.substring(2, 3));
                stringBuffer.append(encryptMaCode.substring(4, 5));
                stringBuffer.append(encryptMaCode.substring(6, 7));
                encryptMaCode = stringBuffer.toString();
            }
            while (encryptMaCode.length() > 16) {
                encryptMaCode = encryptMaCode.substring(0, 16);
            }

            hashMap.put("ma_code", encryptMaCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hashMap;
    }

    public Map<String, String> getEndLecDate(HttpServletRequest request) throws ParseException {
        Map<String, String> map = new HashMap<String, String>();


        Map<String, String> authInfoMap = getAuthInfo(request);
        String endLecDateStr = authInfoMap.get("endLecDateStr");


        String lecDateStr = authInfoMap.get("lecDateStr");
        //用户数
        String users = authInfoMap.get("users");
        lecDateStr = lecDateStr + " 00:00:00";
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate1 = sdf2.parse(lecDateStr);
        Long addTime = 365 * 24 * 60 * 60 * 1000L;
        Long time = startDate1.getTime() + addTime;
        Date endDate = new Date(time);
        String endDateStr = sdf2.format(endDate);
        endDateStr = endDateStr.substring(0, 10);


        String isAuthStr = authInfoMap.get("isAuthStr");
        String useEndDate;
        if ("true".equals(isAuthStr)) {
            //永久使用
            useEndDate = "0000-00-00";
        } else {


        }
        //使用截止日期
        if ("true".equals(isAuthStr)) {
            //永久使用为null
            map.put("useEndDate", "0000-00-00");
        } else {

            String installDateStr = authInfoMap.get("installDate");
            installDateStr = installDateStr + " 00:00:00";
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startDate = sdf1.parse(installDateStr);
            String tryDay = authInfoMap.get("tryDay");

            //默认给个值。
            int trydayTime = 30;
            try {
                trydayTime = Integer.parseInt(tryDay);
            } catch (Exception e) {
            }

            Long addTime1 = trydayTime * 24 * 60 * 60 * 1000L;
            Long time1 = startDate.getTime() + addTime1;
            Date endDate1 = new Date(time1);
            String endDateStr1 = sdf1.format(endDate1);
            useEndDate = endDateStr1.substring(0, 10);
            map.put("useEndDate", useEndDate);

        }

        map.put("endLecDateStr", authInfoMap.get("endDay"));
        map.put("isAuthStr", isAuthStr);
        //用户数
        map.put("usercount", users);
        return map;
    }

    @Override
    public Map<String, String> getAPPMessage() {
        Map<String, String> maps = new HashMap<String, String>();
        SysPara sysPara = sysParaMapper.querySysPara("APP_Android_Version");
        SysPara sysPara1 = sysParaMapper.querySysPara("APP_IOS_Version");
        SysPara sysPcVesison = sysParaMapper.querySysPara("APP_PC_Version");
        SysPara sysPcUrl = sysParaMapper.querySysPara("APP_PC_DownUrl");
        maps.put("softVersionAndroidNO", sysPara.getParaValue());
        maps.put("softVersionIOSNO", sysPara1.getParaValue());
        maps.put("filePath", "file/apk/android/Xoa.apk");
        maps.put("softVersionPCNO", sysPcVesison.getParaValue());
        maps.put("pcfilePath", sysPcUrl.getParaValue());
        return maps;
    }

    @Override
    public Map<String, String> getAuthorization(HttpServletRequest request) {
        Map<String, String> maps = getAuthInfo(request);

        return maps;
    }

    //校验授权模块 1,2,3
    @Override
    public Map checkModule(HttpServletRequest request, String moustr) {
        Map moudleMap = new HashedMap();
        Map resMap = new HashedMap();
        int count = 0;
        String authMoudle = getAuthInfo(request).get("module");
        if (moustr.indexOf(",") > -1 && !StringUtils.checkNull(moustr)) {

            String[] mouArr = moustr.split(",");
            for (String mou : mouArr) {
                if (!StringUtils.checkNull(mou) && authMoudle.indexOf(mou) > -1) {

                    moudleMap.put(mou, true);
                } else {
                    count++;
                    moudleMap.put(mou, false);
                }
            }
        } else if (!StringUtils.checkNull(moustr)) {
            if (!StringUtils.checkNull(moustr) && authMoudle.indexOf(moustr) > -1) {

                moudleMap.put(moustr, true);
            } else {
                count++;
                moudleMap.put(moustr, false);
            }

        } else {
            count++;
        }
        resMap.put("detail", moudleMap);

        resMap.put("result", count == 0 ? true : false);
        return resMap;
    }

    @Override
    public String getAuthModule(HttpServletRequest request) {
        Map authinfo = getAuthInfo(request);
        String moudleStr = "";
        try {
            moudleStr = (String) authinfo.get("module");
        } catch (Exception e) {
            moudleStr = "";
        }
        return moudleStr;
    }


    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/6/29 11:29
     * @函数介绍: 验证本机的机器码和给的机器吗是否一致
     * @参数说明: @param authMachineCode
     * @return: boolean ,true 表示一致
     */
    @Override
    public boolean checkMachineCode(String authMachineCode) throws Exception {

        //机器码16位 因为发现注册机生成的机器码后四位和产品生成机器码的后四位方法竟然很奇葩的不一致，故添加AAAA
        String machineCode = "4CCC6A6247B84C66";
        String encrypt = DESUtils.encrypt(machineCode, NEW_KEY2020);
        //获取本机机器码，拼接或追加的16位,网卡不同对应不同的mac地址。
        if (encrypt != null && encrypt.equals(DESUtils.encrypt(authMachineCode, NEW_KEY2020))) {
            return true;
        }
        return false;
    }

    /**
     * @创建作者: 韩成冰
     * @创建日期: 2017/6/29 18:17
     * @函数介绍: 获取用户人数
     * @参数说明: 无
     * @return: boolean
     **/
    @Override
    public int getAuthUser(HttpServletRequest request) {
        StringBuffer sb = new StringBuffer();
        String realPath = request.getSession().getServletContext().getRealPath("/");

        File file = new File(realPath.concat("/authfiles/xoa2020.dat"));
        if (file == null) {
            file = new File(realPath.concat("/authfiles/lec.txt"));
        }
        //BufferedReader br = null;
        Map<String, String> hashMap = null;
        try (FileReader fileReader = new FileReader(file);
             BufferedReader br = new BufferedReader(fileReader);) {

            String s;
            while ((s = br.readLine()) != null) {
                sb.append(s);
            }


            Map<String, String> map = new HashMap<String, String>();
            String decryptData = DESUtils.finalDec(sb.toString());

            String[] infoArr = decryptData.split(",");


            String userCount = infoArr[2];

            //0表示不限制，
            if (Integer.parseInt(userCount) == 0) {
                return 99999999;
            }
            return Integer.parseInt(userCount);

        } catch (Exception e) {
            e.printStackTrace();
        }


        return 0;
    }

    @Override
    public int getOrgManage(HttpServletRequest request) {
        StringBuffer sb = new StringBuffer();
        String realPath = request.getSession().getServletContext().getRealPath("/");

        File file = new File(realPath.concat("/authfiles/xoa2020.dat"));
        if (file == null) {
            file = new File(realPath.concat("/authfiles/lec.txt"));
        }
        //BufferedReader br = null;
        Map<String, String> hashMap = null;
        try (FileReader fileReader = new FileReader(file);
             BufferedReader br = new BufferedReader(fileReader);) {

            String s;
            while ((s = br.readLine()) != null) {
                sb.append(s);
            }


            Map<String, String> map = new HashMap<String, String>();
            String decryptData = DESUtils.finalDec(sb.toString());

            String[] infoArr = decryptData.split(",");

            String userCount = infoArr[1];


            //0表示不限制，
            if (Integer.parseInt(userCount) == 0) {
                return 99999999;
            }
            return Integer.parseInt(userCount);

        } catch (Exception e) {
            e.printStackTrace();
        }


        return 0;
    }

    public String getSoftName(boolean isAuth) {
        return softName;
    }

    /**
     * @创建作者: zhanglijun
     * @创建日期: 2019/4/17 20:32
     * @函数介绍: 计算两个时间之间的天数，比如处理2017-5-27:22:22:22 到2017-5-28:22:22:22之间是2天。
     * @参数说明: @param startDay 日志开始的时间
     * @return: int
     **/
    public int getDay(Date startDay) throws ParseException {

        int totalDay = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startDayStr = sdf.format(startDay);
        Date startDate = sdf.parse(startDayStr);
        long startSeconds = startDate.getTime();
        String c = String.valueOf(startSeconds);
        c = c.substring(0, c.length() - 3);
        startSeconds = Long.valueOf(c);

        Date date = new Date();
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd");
        String endTodayStr = sdfs.format(date);
        Date endDate = sdfs.parse(endTodayStr);
        long endSeconds = endDate.getTime();
        String v = String.valueOf(endSeconds);
        v = v.substring(0, v.length() - 3);
        endSeconds = Long.valueOf(v);
        totalDay = (int) (startSeconds - endSeconds);

        return totalDay;
    }

    //TEST  lr  注册
    public Map<String, String> getAuthInfo(HttpServletRequest request) {
        String path = request.getSession().getServletContext().getRealPath("/");
        return getAuthInfo(path);
    }


    public String getMachineCode16(){
        //机器码16位
        String machineCode = null;
        ResultSet rs = null;
        try {
            Properties props = Resources.getResourceAsProperties("jdbc-sql.properties");
            String url = props.getProperty("url1001");

            String driver = props.getProperty("driverClassName");
            String username = props.getProperty("uname1001");
            String password = props.getProperty("password1001");
            Class.forName(driver).newInstance();
            Connection conn = (Connection) DriverManager.getConnection(url, username, password);

            String sql = " SELECT * " +
                    "      FROM sys_para" +
                    "      where PARA_NAME='PUSH_MNO' and PARA_VALUE is not null;";
            Statement st = conn.createStatement();
            rs = st.executeQuery(sql);//执行SQL语句
            if (rs.next()) {
                machineCode = rs.getString("PARA_VALUE");
            } else {
                machineCode = MachineCode.get16CharMacsType() == null ? "" : MachineCode.get16CharMacsType().get(0);
                SysPara sysPara = new SysPara();
                sysPara.setParaName("PUSH_MNO");
                sysPara.setParaValue(machineCode);
                    sysParaMapper.insertSysPara(sysPara);
            }

        }catch (Exception e){

        }
        return machineCode;
    }

    /**
     * 获取授权信息加密串
     * @param pwd
     * @return
     */
    public static String newDec(String pwd) {
        String[] allPwd = pwd.split("\\@123456789qwertyuiopasdfghjklzxcvbnm,\\.-\\@");
        String nochinesePwd = allPwd[1];
        String noChineseMsg = null;

        try {
            noChineseMsg =DESUtils.myDecrypt(nochinesePwd, NEW_KEY2020);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return noChineseMsg;
    }

}
